<template>
    <div style="margin:0 10px;">
    <div style="height:50px"></div>
    <!-- 标题 -->
    <div class="bt">
        <h3> {{list[0].title}} </h3>
        <div class="fl"> {{list[0].add_time.substring(0,10)}} </div>
        <div class="rl"> 点击数：{{list[0].click}} </div>
    </div>
    <!-- 内容 -->
    <div>
        <template>
            <div v-html="this.content" style="word-wrap:break-word;white-space:pre-line;text-align:justify;">{{content}}</div>
        </template>
    </div>
    <!-- 留言区 -->
    <h3 style="font-size:16px;margin:20px 0;font-weight:700;">发表评论</h3>
    <hr>
    <div style="border-width:1px;width:100%;margin:10px 0 5px 0;" class="CellGroup">
      <van-cell-group>
        <van-field v-model="value" placeholder="请输入留言" style="height:78px;"/>
      </van-cell-group>
    </div>
    <van-button type="info"  style="width:100%;" @click="faBiao">发表评论</van-button>
    <!-- 评论区 -->
    <div class="comment" v-for="(item,i) in listCont" :key="i">
      <div class="com_name">
        <span>第 {{i + 1}}楼</span>
        <span> {{item.user_name}} </span>
        <span> 发表时间：{{item.add_time.substring(0,10)}} </span>
      </div>
      <div class="com_cont"> {{item.content}} </div>
    </div>
    <!-- 加载更多 -->
    <van-button type="default" style="color:red;width:100%;border-color:red;height:45px;" @click="load">加载更多</van-button>
    <!--  -->
    <div style="height:50px"></div>
    </div>
</template>
<script>
export default {
  data () {
    return {
      list: [],
      listCont: [],
      content: '',
      value: '',
      vals: '',
      page: 1
    }
  },
  created () {
    this.getUrl()
    this.getComment()
  },
  methods: {
    async getUrl () {
      const index = this.$route.path
      const vals = index.substring(9)
      this.vals = vals
      const { data: res } = await this.$axios.get('/api/getnew/' + vals)
      console.log(res)
      this.list = res.message
      console.log(this.list)
      this.content = this.list[0].content
    },
    // 获取评论
    async getComment () {
      const { data: res } = await this.$axios.get('api/getcomments/ ' + this.vals + ' ?pageindex= ' + this.page)
      if (this.page === 0) {
        this.listCont = res.message
      }
      this.listCont = this.listCont.concat(res.message)
      console.log(this.listCont)
    },
    // 发表评论
    faBiao () {
      const tex = {
        content: this.value,
        add_time: this.listCont[0].add_time,
        user_name: this.listCont[0].user_name
      }
      console.log(tex)
      this.listCont.unshift(tex)
      console.log(this.listCont)
      this.value = ''
    },
    load () {
      this.page++
      this.getComment()
    }
  }
}
</script>
<style scoped lang="less">
.bt {
    margin: 0 10px;
    padding-bottom: 10px;
    overflow: hidden;
    border-bottom: 1px solid #ccc;
}
.bt h3{
    height: 40px;
    color: #1983FA;
    font-size: 14px;
    text-align: center;
    line-height: 40px;
}
.fl{
    float: left;
    color: #1983FA;
    font-size: 13px;
}
.rl{
    float: right;
    color: #1983FA;
    font-size: 13px;
}
.CellGroup{
    border: #ccc solid 1px;
}
.comment{
  .com_name{
    height: 30px;
    background-color: #ccc;
    font-size: 12px;
    line-height: 30px;
  }
  .com_cont{
    line-height: 35px;
    font-size: 12px;
  }
}
</style>
